package com.sx.service.design;

import com.sx.basic.ResponseEntity;
import com.sx.basic.service.BasicService;
import com.sx.basic.service.impl.BasicServiceImpl;
import com.sx.dao.design.DesignModuleRepository;
import com.sx.design.IDesignModuleService;
import com.sx.design.IDesignParamsService;
import com.sx.entity.design.DesignModuleEntity;
import com.sx.entity.design.DesignParamsEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

/**
 * @author AdinZ
 * @date 2020/11/18 10:12
 */
@Service
public class DesignModuleService extends BasicServiceImpl<DesignModuleRepository, DesignModuleEntity,Long>
        implements BasicService<DesignModuleEntity,Long>, IDesignModuleService {


    @Resource
    private IDesignParamsService designParamsService;

    @Override @Transactional
    public ResponseEntity putDesignModule(DesignModuleEntity entity) {
        if (entity == null) return toError();
        List<DesignParamsEntity> list = entity.getList();
        if (entity.getId() != null) {
            this.designParamsService.deleteByModuleId(entity.getId());
        } else {
            this.save(entity);
        }
        if (list != null && list.size() > 0)
            this.designParamsService.putParams(entity.getId(), list);
        this.redisService.delete(DesignModuleEntity.KEY,entity.getId());
        return toSuccess();
    }

    @Override
    public DesignModuleEntity getDetail(Long id) {

        DesignModuleEntity entity=this.redisService.get(DesignModuleEntity.KEY,id);
        if(entity==null) {
            entity = this.get(id);
            if (entity == null) return null;
            List<DesignParamsEntity> list = this.designParamsService.findTree(entity.getId());
            entity.setList(list);
            this.redisService.set(DesignModuleEntity.KEY,entity.getId(),entity);
        }
        return entity;
    }
}
